﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; 
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace XYHImage
{
    public partial class Main : Form
    {
        private System.String StrFileName = "";
        private Bitmap sourcetemp;
        public Main()
        {
            InitializeComponent();
        }

        private void button18_Click(object sender, EventArgs e)
        {
           
            //来自剪切板
            int flag = 0;
            try
            {
                IDataObject mydata = Clipboard.GetDataObject();
                if (mydata.GetDataPresent(DataFormats.Bitmap))
                {

                    System.String strfilename = Application.StartupPath + "\\screen";
                    if (strfilename.Trim() == "")
                    {
                        return;
                    }
                    else
                    {
                        Bitmap map1 = (Bitmap)mydata.GetData(DataFormats.Bitmap);
                        map1.Save(strfilename + ".png", System.Drawing.Imaging.ImageFormat.Png);
                        map1.Dispose();
                    }
                    flag = 1;
                }
                else
                {
                    MessageBox.Show("系统剪贴板没有可编辑的图像!\r\n请先用 Alt + PrintScreen 或 Win + shift + s 抓屏！", "向阳花图像处理工具", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
               
                 
            }
            catch
            {
            }
            if (flag == 0)
            {
                return;
            }
            string StrFileName1 = Application.StartupPath + "\\screen.png";
            System.Drawing.Bitmap MyBitmap = new Bitmap(StrFileName1);
            this.numericUpDown3.Value = MyBitmap.Width;//长
            this.numericUpDown4.Value = MyBitmap.Height;//宽
            this.pictureBox1.Width = (int)this.numericUpDown3.Value;
            this.pictureBox1.Height = (int)this.numericUpDown4.Value;
            this.pictureBox1.Image = MyBitmap;
            this.numericUpDown7.Value = 100;
            this.label6.Text = MyBitmap.Height.ToString().Trim();//长
            this.label4.Text = MyBitmap.Width.ToString().Trim(); //宽
                                                                 //以下开启控件
            this.textBox1.Enabled = true;
            this.textBox3.Enabled = true;
            this.textBox4.Enabled = true;
            this.comboBox1.Enabled = true;
          
            this.trackBar2.Enabled = true;
            this.numericUpDown7.Enabled = true;
            this.numericUpDown3.Enabled = true;
            this.numericUpDown4.Enabled = true;

          
            this.button2.Enabled = true;
            this.button4.Enabled = true;
            this.button5.Enabled = true;
            this.button6.Enabled = true;
            
            this.button13.Enabled = true;
            this.button14.Enabled = true;

            try
            {

                System.Drawing.Bitmap MyBitmap1 = new Bitmap(StrFileName1);
                 sourcetemp = new Bitmap(MyBitmap1, (int)this.numericUpDown3.Value, (int)this.numericUpDown4.Value);
                 
                this.pictureBox1.Image = sourcetemp;
                MyBitmap.Dispose();
                MyBitmap1.Dispose();               
                this.pictureBox1.Refresh();
                tiaozheng();
                 
                this.Text = StrFileName1.Trim();
                
            }
            catch
            {
                MessageBox.Show(this, "打开图像发生错误！", "向阳花图像处理工具", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
             GC.Collect();
        }
        private void tiaozheng()
        {
            if (pictureBox1.Width > 650)
            {
                float a1, a2;
                a1 = 600;
                a2 = pictureBox1.Width;
                int aa = (int)(a1 / a2 * 100);
                if (aa < 10)
                { this.numericUpDown7.Value = 10; }
                else
                { this.numericUpDown7.Value = aa; }

            }
        }
        private System.Drawing.Point mystartpos;
        private System.Drawing.Point myendpos;
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            label20.Text = ((int)(int.Parse(e.X.ToString()) * 100 / (int)this.numericUpDown7.Value)).ToString().Trim();
            label21.Text = ((int)(int.Parse(e.Y.ToString()) * 100 / (int)this.numericUpDown7.Value)).ToString().Trim();
            if (button14.ForeColor == Color.Red && e.Button == MouseButtons.Left)
            {
                myendpos.X = e.X;
                myendpos.Y = e.Y;
                this.pictureBox1.Refresh();
                label20.Refresh();
                label21.Refresh();
            }
            if (button13.ForeColor == Color.Green && e.Button == MouseButtons.Left)
            {
                myendpos.X = mystartpos.X + int.Parse(textBox3.Text);
                myendpos.Y = mystartpos.Y + int.Parse(textBox4.Text);
                this.pictureBox1.Refresh();
                label20.Refresh();
                label21.Refresh();
            }
          
        }

        private void numericUpDown7_ValueChanged(object sender, EventArgs e)
        {
            try
            { 
                    this.pictureBox1.Width = (int)((int)this.numericUpDown3.Value * (int)this.numericUpDown7.Value / 100);
                    this.pictureBox1.Height = (int)((int)this.numericUpDown4.Value * (int)this.numericUpDown7.Value / 100);
                
            }
            catch
            {
                this.numericUpDown7.Value = 100;
            }
        }
      
        private void button5_Click(object sender, EventArgs e)
        {
            //长基调整
            try
            {
                this.numericUpDown4.Value = (int)((int)this.numericUpDown3.Value * int.Parse(label6.Text) / int.Parse(label4.Text));
                //System.Drawing.Bitmap MyBitmap1 = new Bitmap(sourcetemp);// new Bitmap(this.StrFileName);
                //sourcetemp = new Bitmap(MyBitmap1, (int)this.numericUpDown3.Value, (int)this.numericUpDown4.Value);
                //this.pictureBox1.Image = sourcetemp;
                //this.numericUpDown7.Value = 100;
              
                //this.pictureBox1.Width = (int)((int)this.numericUpDown3.Value * (int)this.numericUpDown7.Value / 100);
                //this.pictureBox1.Height = (int)((int)this.numericUpDown4.Value * (int)this.numericUpDown7.Value / 100); 
               
                //this.pictureBox1.Refresh();
                
            }
            catch
            {
                MessageBox.Show(this, "转换图像大小时发生错误！请输入有效数值！", "向阳花图像处理工具", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //宽基调整
            try
            {
                this.numericUpDown3.Value = (int)((int)this.numericUpDown4.Value * int.Parse(label4.Text) / int.Parse(label6.Text));

                //System.Drawing.Bitmap MyBitmap1 = new Bitmap(sourcetemp);
                //sourcetemp = new Bitmap(MyBitmap1, (int)this.numericUpDown3.Value, (int)this.numericUpDown4.Value);
                //this.pictureBox1.Image = sourcetemp;
                //this.numericUpDown7.Value = 100;
              
                //this.pictureBox1.Width = (int)((int)this.numericUpDown3.Value * (int)this.numericUpDown7.Value / 100);
                //this.pictureBox1.Height = (int)((int)this.numericUpDown4.Value * (int)this.numericUpDown7.Value / 100);

                 
               // this.pictureBox1.Refresh();
            }
            catch
            {
                MessageBox.Show(this, "转换图像大小时发生错误！请输入有效数值！", "向阳花图像处理工具", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //按长宽调整
            if (isnumber(this.textBox1.Text))
            {
                System.Drawing.Bitmap MyBitmap1 = new Bitmap(sourcetemp);//new Bitmap(this.StrFileName);
                sourcetemp = new Bitmap(MyBitmap1, (int)this.numericUpDown3.Value, (int)this.numericUpDown4.Value);
                MyBitmap1.Dispose();
                int DPI = int.Parse(this.textBox1.Text);
                sourcetemp.SetResolution(DPI, DPI);
                this.pictureBox1.Image = sourcetemp;
                scanf();
            }else
            {
                MessageBox.Show("请设为数值型");
            }
            this.pictureBox1.Refresh();
            GC.Collect();
           
        }
        private bool isnumber(string str1)
        {
            if (str1.Trim() == "") return false;
            for (int i = 0; i <= str1.Length - 1; i++)
            {
                if (str1.Substring(i, 1).Trim() != ".")
                {
                    if (!System.Char.IsNumber(str1, i))
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        private void button13_Click(object sender, EventArgs e)
        {
            //指定大小剪切
            if (numericUpDown7.Value == 100)
            {
                if (isnumber(textBox3.Text.Trim()) && isnumber(textBox4.Text.Trim()) && int.Parse(textBox4.Text) > 0 && int.Parse(textBox3.Text) > 0)
                {
                    this.button13.ForeColor = Color.Green;
                    this.pictureBox1.Cursor = Cursors.Cross;
                }
                else
                {
                    textBox4.Text = "100";
                    textBox3.Text = "100";
                    MessageBox.Show("照片尺寸应为大于0的数值型数据！", "向阳花图像处理工具", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }else
            {
                MessageBox.Show("使用固定切图前，请调整图像缩放比例为100%", "向阳花图像处理工具", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            //鼠标拖放剪切
            this.button14.ForeColor = Color.Red;
            this.pictureBox1.Cursor = Cursors.Cross;
           
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            //拖放剪切
            if (button14.ForeColor == Color.Red && e.Button == MouseButtons.Left)
            {
                mystartpos.X = e.X;
                mystartpos.Y = e.Y;

            }
            if (button13.ForeColor == Color.Green && e.Button == MouseButtons.Left)
            {
                mystartpos.X = e.X;
                mystartpos.Y = e.Y;

            }
            GC.Collect();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            if (g == null) return;            
            //拖放剪切,显示选择区（红色虚线）
            if (button14.ForeColor == Color.Red && MouseButtons == MouseButtons.Left)
            {
                Pen mypen = new Pen(System.Drawing.Brushes.Red, 1);
                mypen.DashStyle = DashStyle.Dot;

                int x1, x2, y1, y2;
                x1 = mystartpos.X;
                y1 = mystartpos.Y;
                x2 = myendpos.X;
                y2 = myendpos.Y;
                g.DrawRectangle(mypen, x1, y1, x2 - x1 + 1, y2 - y1 + 1);

            }
            if (button13.ForeColor == Color.Green && MouseButtons == MouseButtons.Left)
            {
                Pen mypen = new Pen(System.Drawing.Brushes.Green, 1);
                mypen.DashStyle = DashStyle.Dot;

                int x1, x2, y1, y2;
                x1 = mystartpos.X;
                y1 = mystartpos.Y;
                x2 = myendpos.X;
                y2 = myendpos.Y;
               g.DrawRectangle(mypen, x1, y1, x2 - x1 + 1, y2 - y1 + 1);

            }
           
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            
            //剪切选区为新图
            if (button14.ForeColor == Color.Red && e.Button == MouseButtons.Left)
            {
                //鼠标自定义剪切
                this.button14.ForeColor = Color.Black;
                int x1, x2, y1, y2;
                x1 = mystartpos.X;
                y1 = mystartpos.Y;
                x2 = myendpos.X;
                y2 = myendpos.Y;
                x1 = x1 * 100 / (int)this.numericUpDown7.Value;
                x2 = x2 * 100 / (int)this.numericUpDown7.Value;
                y1 = y1 * 100 / (int)this.numericUpDown7.Value;
                y2 = y2 * 100 / (int)this.numericUpDown7.Value;
                if (x2 > this.pictureBox1.Image.Width)
                {
                    x2 = this.pictureBox1.Image.Width;
                }
                if (y2 > this.pictureBox1.Image.Height)
                {
                    y2 = this.pictureBox1.Image.Height;
                }
                int width = x2 - x1 + 1;//剪切图像高宽
                int heigth = y2 - y1 + 1;
                if (width != 0 && heigth != 0 && width > 0 && heigth > 0)
                {
                    this.numericUpDown7.Value = 100;
                    System.Drawing.Bitmap MyBitmap1 = new Bitmap(Application.StartupPath + "\\back.png");
                    Bitmap my1 = new Bitmap(MyBitmap1, width, heigth);
                   
                    Graphics g1 = Graphics.FromImage(my1);
                    System.Drawing.Bitmap Bitmap1 = new Bitmap(this.pictureBox1.Image);
                    TextureBrush mb = new TextureBrush(Bitmap1, Rectangle.FromLTRB(x1, y1, x2, y2));
                    g1.FillRectangle(mb, Rectangle.FromLTRB(0, 0, width, heigth)); 
                    
                    sourcetemp = new Bitmap(my1);
                    this.pictureBox1.Image =sourcetemp;
                    this.pictureBox1.Width = this.pictureBox1.Image.Width;
                    this.pictureBox1.Height = this.pictureBox1.Image.Height;
                    this.numericUpDown3.Value = this.pictureBox1.Image.Width;
                    this.numericUpDown4.Value = this.pictureBox1.Image.Height; 
                    
                    MyBitmap1.Dispose();
                    Bitmap1.Dispose();
                    mb.Dispose();
                    g1.Dispose();
                    my1.Dispose();
                }
                this.pictureBox1.Refresh(); 
            }
            if (button13.ForeColor == Color.Green && e.Button == MouseButtons.Left)
            {
                //指定尺寸剪切
                this.button13.ForeColor = Color.Black;
                int x1, x2, y1, y2;
                x1 = mystartpos.X;
                y1 = mystartpos.Y;
                x2 = myendpos.X;
                y2 = myendpos.Y;
                x1 = x1 * 100 / (int)this.numericUpDown7.Value;
                x2 = x2 * 100 / (int)this.numericUpDown7.Value;
                y1 = y1 * 100 / (int)this.numericUpDown7.Value;
                y2 = y2 * 100 / (int)this.numericUpDown7.Value;
                if (x2 > this.pictureBox1.Image.Width)
                {
                    x2 = this.pictureBox1.Image.Width;
                }
                if (y2 > this.pictureBox1.Image.Height)
                {
                    y2 = this.pictureBox1.Image.Height;
                }
                int width = x2 - x1 + 1;//剪切图像高宽
                int heigth = y2 - y1 + 1;
                if (width != 0 && heigth != 0 && width > 0 && heigth > 0)
                {
                    this.numericUpDown7.Value = 100;
                    System.Drawing.Bitmap MyBitmap1 = new Bitmap(Application.StartupPath + "\\back.png");
                    Bitmap my1 = new Bitmap(MyBitmap1, width, heigth);
                    Graphics g1 = Graphics.FromImage(my1);
                    System.Drawing.Bitmap Bitmap1 = new Bitmap(this.pictureBox1.Image);
                    TextureBrush mb = new TextureBrush(Bitmap1, Rectangle.FromLTRB(x1, y1, x2, y2));
                    g1.FillRectangle(mb, Rectangle.FromLTRB(0, 0, width, heigth));
                   
                    sourcetemp = new Bitmap(my1);
                    this.pictureBox1.Image = sourcetemp;
                    this.pictureBox1.Width = this.pictureBox1.Image.Width;
                    this.pictureBox1.Height = this.pictureBox1.Image.Height;
                    this.numericUpDown3.Value = this.pictureBox1.Image.Width;
                    this.numericUpDown4.Value = this.pictureBox1.Image.Height;
                    mb.Dispose();
                    g1.Dispose();
                    MyBitmap1.Dispose();
                    Bitmap1.Dispose();
                    my1.Dispose();

                }
                this.pictureBox1.Refresh();
               
            }
            scanf();
           
            GC.Collect();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            //打开
            try
            {
                this.openFileDialog1.ShowDialog();
                this.StrFileName = this.openFileDialog1.FileName;
                this.openFileDialog1.Dispose();
                if (this.StrFileName.Trim() == "")
                    return; 
                System.Drawing.Bitmap MyBitmap = new Bitmap(this.StrFileName); 
                this.numericUpDown3.Value = MyBitmap.Width;//长
                this.numericUpDown4.Value = MyBitmap.Height;//宽
                this.pictureBox1.Width = (int)this.numericUpDown3.Value;
                this.pictureBox1.Height = (int)this.numericUpDown4.Value;
                this.pictureBox1.Image = MyBitmap;
                this.textBox1.Text =((int)MyBitmap.VerticalResolution).ToString();//dpi
                this.numericUpDown7.Value = 100;
                this.label6.Text = MyBitmap.Height.ToString().Trim();//长
                this.label4.Text = MyBitmap.Width.ToString().Trim(); //宽
                MyBitmap.Dispose();
                //以下开启控件
                this.textBox1.Enabled = true;
                this.textBox3.Enabled = true;
                this.textBox4.Enabled = true;
                this.comboBox1.Enabled = true;
                this.trackBar2.Enabled = true;
                this.numericUpDown7.Enabled = true;
                this.numericUpDown3.Enabled = true;
                this.numericUpDown4.Enabled = true;


                this.button2.Enabled = true;
                this.button4.Enabled = true;
                this.button5.Enabled = true;
                this.button6.Enabled = true;

                this.button13.Enabled = true;
                this.button14.Enabled = true;



            }
            catch
            {
                MessageBox.Show(this, "无效的图像文件！", "向阳花图像处理工具", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            try
            {

                System.Drawing.Bitmap MyBitmap1 = new Bitmap(this.StrFileName);

                Bitmap NewBitmap = new Bitmap(MyBitmap1, (int)this.numericUpDown3.Value, (int)this.numericUpDown4.Value);

                sourcetemp = new Bitmap(NewBitmap);
                //sourcetempBK = new Bitmap(sourcetemp);

                this.pictureBox1.Image = sourcetemp;

                this.pictureBox1.Refresh();
                
                this.Text = this.StrFileName.Trim();
                scanf();
                NewBitmap.Dispose();
                MyBitmap1.Dispose();
               


            }
            catch
            {
                MessageBox.Show(this, "打开图像时发生错误！", "向阳花图像处理工具", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            GC.Collect();
        }
      private void  scanf()
        {
            if (pictureBox1.Width * 100 > splitContainer1.Panel2.Width * 95)
            {
                int a2 = splitContainer1.Panel2.Width * 90;
                int a1 = a2 / pictureBox1.Width;
                this.numericUpDown7.Value = a1;
            }           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);
            this.pictureBox1.MouseWheel += PictureBox1_MouseWheel;
        }

        private void PictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Delta > 0)
                {
                    //放大
                    this.numericUpDown7.Value=this.numericUpDown7.Value + 2;
                }
                else
                {
                    if(this.numericUpDown7.Value>12)
                    //缩小
                    this.numericUpDown7.Value=this.numericUpDown7.Value-2;
                }
            }
            catch { this.numericUpDown7.Value = 100; }
        }

        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            label26.Text = this.trackBar2.Value.ToString().Trim();
        }

        private void trackBar2_ValueChanged(object sender, EventArgs e)
        {
            computerbyte();
        }
        private void computerbyte()//计算JPG文件大小
        {
            int Qty = trackBar2.Value;//压缩质量因子1-100
            EncoderParameter p1;
            EncoderParameters ps;
            ps = new EncoderParameters(1);
            p1 = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Qty);
            // p2 = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 16);//位深,jpg只有24位深
            ps.Param[0] = p1;
            MemoryStream ms = new MemoryStream();
            this.pictureBox1.Image.Save(ms, GetCodecInfo("image/jpeg"), ps);
            float l1 = ms.Length / 1024;
            label25.Text = l1.ToString() + "Kb(" + ms.Length.ToString().Trim() + "字节)";
           
        }
        private static ImageCodecInfo GetCodecInfo(string mimeType)
        {
            ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
            foreach (ImageCodecInfo ici in CodecInfo)
            {
                if (ici.MimeType == mimeType) return ici;
            }
            return null;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //另存为

            if (this.saveFileDialog1.ShowDialog() == DialogResult.Cancel)
                return;
            string StrFileName1 = this.saveFileDialog1.FileName;
            FileInfo fi= new FileInfo(StrFileName1);
            string StrFileExt = fi.Extension; //StrFileName1.Substring(StrFileName1.Length - 3, 3);            
            try
            {
                int flag = 0;
                if (StrFileExt.ToUpper() == ".BMP")
                {
                    this.pictureBox1.Image.Save(StrFileName1, System.Drawing.Imaging.ImageFormat.Bmp);
                    flag = 1;
                }
                if (StrFileExt.ToUpper() == ".GIF")
                {
                    this.pictureBox1.Image.Save(StrFileName1, System.Drawing.Imaging.ImageFormat.Gif);
                    flag = 1;
                }
                if (StrFileExt.ToUpper() == ".JPG" || StrFileExt.ToUpper() == ".JPEG")
                {
                    int Qty = trackBar2.Value;
                    EncoderParameter p1;
                    EncoderParameters ps;
                    ps = new EncoderParameters(1);
                    p1 = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Qty);
                    // p2 = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 16);//位深,jpg只有24位深
                    ps.Param[0] = p1;
                    this.pictureBox1.Image.Save(StrFileName1, GetCodecInfo("image/jpeg"), ps);
                  
                    flag = 1;
                }
                if (StrFileExt.ToUpper() == ".PNG")
                {
                    this.pictureBox1.Image.Save(StrFileName1, System.Drawing.Imaging.ImageFormat.Png);
                    flag = 1;
                }
                if (StrFileExt.ToUpper() == ".TIF" || StrFileExt.ToUpper() == ".TIFF")
                {
                    this.pictureBox1.Image.Save(StrFileName1, System.Drawing.Imaging.ImageFormat.Tiff);
                    flag = 1;
                }
                if (StrFileExt.ToUpper() == ".WMF")
                {
                    this.pictureBox1.Image.Save(StrFileName1, System.Drawing.Imaging.ImageFormat.Wmf);
                    flag = 1;
                }
                if (StrFileExt.ToUpper() == ".ICO")
                {
                    this.pictureBox1.Image.Save(StrFileName1, System.Drawing.Imaging.ImageFormat.Icon);
                    flag = 1;
                }
                if (flag == 0)
                {
                    this.pictureBox1.Image.Save(StrFileName1, System.Drawing.Imaging.ImageFormat.Jpeg);

                }

            }
            catch
            {
                MessageBox.Show(this, "转换图像文件时发生错误！请检查是否有同名文件存在！", "向阳花图像处理工具", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            GC.Collect();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int Qty = trackBar2.Value;
            EncoderParameter p1;
            EncoderParameters ps;
            ps = new EncoderParameters(1); 
            p1 = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Qty);
           // p2 = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 16);//位深,jpg只有24位深
            ps.Param[0] = p1;
       
            this.pictureBox1.Image.Save(Application.StartupPath + "\\screen.jpg", GetCodecInfo("image/jpeg"), ps);
            //Bitmap bmp = new Bitmap(Application.StartupPath + "\\screen.jpg");
            //Clipboard.SetImage(bmp);
            StringCollection c1 = new StringCollection();
            c1.Add(Application.StartupPath + "\\screen.jpg");
            Clipboard.SetFileDropList(c1);
           // bmp.Dispose();
            msg m1 = new msg();
            m1.Parent= this.Parent;
            m1.ShowDialog();
            GC.Collect();
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            //确定拖放的是文件
            if(e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            //拖放事件
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            if(files.Length > 0 )
            {
               
                //打开
                try
                {
                    
                    this.StrFileName = files[0];
                    //FileInfo fi = new FileInfo(StrFileName);
                    //string StrFileExt = fi.Extension; //StrFileName1.Substring(StrFileName1.Length - 3, 3);        
                    System.Drawing.Bitmap MyBitmap = new Bitmap(this.StrFileName);
                    this.numericUpDown3.Value = MyBitmap.Width;//长
                    this.numericUpDown4.Value = MyBitmap.Height;//宽
                    this.pictureBox1.Width = (int)this.numericUpDown3.Value;
                    this.pictureBox1.Height = (int)this.numericUpDown4.Value;
                    this.pictureBox1.Image = MyBitmap;
                    this.textBox1.Text = ((int)MyBitmap.VerticalResolution).ToString();//dpi
                    this.numericUpDown7.Value = 100;
                    this.label6.Text = MyBitmap.Height.ToString().Trim();//长
                    this.label4.Text = MyBitmap.Width.ToString().Trim(); //宽
                    MyBitmap.Dispose();
                    //以下开启控件
                    this.textBox1.Enabled = true;
                    this.textBox3.Enabled = true;
                    this.textBox4.Enabled = true;
                    this.comboBox1.Enabled = true;
                    this.trackBar2.Enabled = true;
                    this.numericUpDown7.Enabled = true;
                    this.numericUpDown3.Enabled = true;
                    this.numericUpDown4.Enabled = true;


                    this.button2.Enabled = true;
                    this.button4.Enabled = true;
                    this.button5.Enabled = true;
                    this.button6.Enabled = true;

                    this.button13.Enabled = true;
                    this.button14.Enabled = true;



                }
                catch
                {
                    MessageBox.Show(this, "无效的图像文件！", "向阳花图像处理工具", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                try
                {

                    System.Drawing.Bitmap MyBitmap1 = new Bitmap(this.StrFileName);

                    Bitmap NewBitmap = new Bitmap(MyBitmap1, (int)this.numericUpDown3.Value, (int)this.numericUpDown4.Value);

                    sourcetemp = new Bitmap(NewBitmap);
                    

                    this.pictureBox1.Image = sourcetemp;

                    this.pictureBox1.Refresh();

                    this.Text = this.StrFileName.Trim();
                    scanf();
                    NewBitmap.Dispose();
                    MyBitmap1.Dispose();



                }
                catch
                {
                    MessageBox.Show(this, "打开图像时发生错误！", "向阳花图像处理工具", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            GC.Collect();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBox1.SelectedIndex)
            {
                case 0:

                    this.groupBox6.Visible = true;
                    this.trackBar1.Value = 0;
                    break;
                case 1:
                    this.groupBox5.Visible = true;
                    this.groupBox5.Refresh();
                    this.comboBox1.Refresh();
                    whiteblank((Bitmap)this.pictureBox1.Image);
                    this.groupBox5.Refresh();
                    this.groupBox5.Visible = false;

                    break;

            }
        }
        private void whiteblank(Bitmap b)
        {

            BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
            ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            int stride = bmData.Stride;
            System.IntPtr Scan0 = bmData.Scan0;
            unsafe
            {
                byte* p = (byte*)(void*)Scan0;
                int nOffset = stride - b.Width * 3;
                byte red, green, blue;
                this.progressBar1.Maximum = b.Height;
                this.progressBar1.Value = 0;
                for (int y = 0; y < b.Height; ++y)
                {
                    for (int x = 0; x < b.Width; ++x)
                    {
                        blue = p[0];
                        green = p[1];
                        red = p[2];
                        p[0] = p[1] = p[2] = (byte)((red + green + blue) / 3);
                        p += 3;
                    }
                    p += nOffset;
                    this.progressBar1.Value = y;
                }
            }
            b.UnlockBits(bmData);
            sourcetemp = b;
            this.progressBar1.Value = 0;
            this.pictureBox1.Refresh(); 
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            Bitmap pbitmap1 = new Bitmap(sourcetemp, sourcetemp.Width, sourcetemp.Height);
            this.pictureBox1.Image = pbitmap1;
            Brightness((Bitmap)this.pictureBox1.Image, (int)this.trackBar1.Value);
        }
        private void Brightness(Bitmap b, int nBrightness)
        {

            if (nBrightness < -255 || nBrightness > 255)
                return;
            BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width,
            b.Height), ImageLockMode.ReadWrite,
            PixelFormat.Format24bppRgb);
            int stride = bmData.Stride;
            System.IntPtr Scan0 = bmData.Scan0;
            int nVal = 0;
            unsafe
            {
                byte* p = (byte*)(void*)Scan0;
                int nOffset = stride - b.Width * 3;
                int nWidth = b.Width * 3;
                for (int y = 0; y < b.Height; ++y)
                {
                    for (int x = 0; x < nWidth; ++x)
                    {
                        nVal = (int)(p[0] + nBrightness);
                        if (nVal < 0) nVal = 0;
                        if (nVal > 255) nVal = 255;
                        p[0] = (byte)nVal;
                        ++p;

                    }
                    p += nOffset;
                }

            }
            b.UnlockBits(bmData);
            
        }

        private void button15_Click(object sender, EventArgs e)
        {
            this.groupBox6.Visible = false;
            sourcetemp = new Bitmap(this.pictureBox1.Image);
        }

        private void button16_Click(object sender, EventArgs e)
        {
            this.pictureBox1.Image = sourcetemp;
            this.groupBox6.Visible = false;
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 打开向阳图像处理工具ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
                this.WindowState = FormWindowState.Normal;
            this.Activate();
            this.ShowInTaskbar = true;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            GC.Collect();
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
            e.Cancel = true;
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
                this.WindowState = FormWindowState.Normal;
            this.Activate();
           this.ShowInTaskbar = true;
        }

        private void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e)
        {
            GC.Collect();
        }
    }
}
